import 'package:flutter/material.dart';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'pkg_test.dart';
import 'notification_test.dart';
import 'theme_test.dart';
//import 'package:flutter/rendering.dart';

void main() {
//  debugPaintSizeEnabled = true;
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '浮游测试',
      theme: ThemeData(
        primarySwatch: Colors.cyan,
        primaryColor: Colors.pink,
        iconTheme: IconThemeData(color: Colors.yellow),
      ),
      home: DemoPage(),
    );
  }
}

class DemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return DefaultTabController(
        length: 4,
        child: Scaffold(
          body: TabBarView(
//            physics: NeverScrollableScrollPhysics(),
            children: [
              MyHomePage(),
              PkgTest(),
              MyNotification(),
              MyThemePage()
            ],
          ),
          bottomNavigationBar: TabBar(
            tabs: [
              Tab(
                icon: Icon(Icons.home),
                text: "jojo",
              ),
              Tab(
                icon: Icon(Icons.rss_feed),
                text: "木大",
              ),
              Tab(
                icon: Icon(Icons.perm_identity),
                text: "欧拉",
              ),
              Tab(icon: Icon(Icons.account_balance_wallet), text: "luffy")
            ],
            unselectedLabelColor: Colors.blueGrey,
            labelColor: Colors.orange,
            indicatorSize: TabBarIndicatorSize.label,
            indicatorColor: Colors.red,
          ),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('gakki $_counter'),
      ),
      body: new ListView(children: <Widget>[
        Text(
            '哈哈哈哈哈，我随便试我我随便试我我随便试我我随便试我我随便试我我随便试我我随便试我我随便试我随便试我随便试我随便试我随便试试122',
            maxLines: 2,
            textAlign: TextAlign.center,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
        Text(
          '字体测试',
          style: TextStyle(fontFamily: 'WebFont'),
        ),
        Icon(Icons.add_shopping_cart),
        Row(
          children: [
            Container(
              color: Colors.yellow,
              width: 60,
              height: 80,
            ),
            Container(
              color: Colors.red,
              width: 100,
              height: 180,
            ),
            Container(
              color: Colors.black,
              width: 60,
              height: 80,
            ),
            Container(
              color: Colors.green,
              width: 60,
              height: 80,
            ),
            Expanded(
              flex: 1,
              child: Container(color: Colors.deepOrange, height: 60),
            ),
            Expanded(
              flex: 1,
              child: Container(color: Colors.orange, height: 60),
            ),
          ],
        ),
        Stack(children: <Widget>[
          Container(color: Colors.yellow, width: 300, height: 300),
          Positioned(
              left: 18.0,
              top: 18.0,
              child: Container(
                color: Colors.green,
                width: 50,
                height: 50,
              )),
          Positioned(left: 18.0, top: 70.0, child: Text("Stack提供了层叠布局的容器"))
        ]),
        Container(
            child: Text('Container 试一试啦啦啦'),
            width: 90,
            height: 200,
            margin: EdgeInsets.all(44.0),
            decoration: BoxDecoration(color: Colors.purple)),
        Cake(),
        Image.asset('assets/images/gakki2.jpeg'),
        Image.network(
          'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577113444568&di=8e61dbf16be7f1722265f288db6c58e3&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201712%2F18%2F20171218232452_2anMf.jpeg',
          fit: BoxFit.cover,
          width: 200,
          height: 300,
        ),
        Image.network(
            'https://cbu01.alicdn.com/img/ibank/2017/647/400/4377004746_172792957.jpg?__r__=1498706922436',
            fit: BoxFit.cover,
            width: 200,
            height: 300),
        Image.network(
            'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577113532784&di=b7e7a76edbb70f543849724c63568360&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201706%2F06%2F20170606014942_eH52r.thumb.700_0.jpeg',
            fit: BoxFit.cover,
            width: 200,
            height: 500),
        Image.network('http://i.shangc.net/2017/0312/20170312095912743.jpg',
            repeat: ImageRepeat.repeat),
        Image.network(
            'http://5b0988e595225.cdn.sohucs.com/images/20191028/b55c71be7ed648a0b364fb63c888be64.jpeg'),
        ListTile(leading: Icon(Icons.map), title: Text('古来圣贤皆寂寞')),
        ListTile(leading: Icon(Icons.account_box), title: Text('唯有饮者留其名')),
        FloatingActionButton(
          onPressed: () => print('FloatingActionButton pressed'),
          child: Text('Btn'),
        ),
        FlatButton(
          color: Colors.yellow,
          colorBrightness: Brightness.light,
          onPressed: () => print('FlatButton pressed'),
          shape:
              BeveledRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
          child: Text('Btn'),
        ),
        RaisedButton(
            onPressed: () => print('RaisedButton pressed'),
            child: Row(
              children: [Icon(Icons.add), Text("Add2")],
            ))
      ]),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class WheelPainter extends CustomPainter {
  WheelPainter({this.a});

  List<double> a = [];

  // 设置画笔颜色
  Paint getColoredPaint() {
    //根据颜色返回不同的画笔
    Paint paint = Paint(); //生成画笔
    paint.color = Color((Random().nextDouble() * 0xFFFFFF).toInt() << 0)
        .withOpacity(1.0); //设置画笔颜色
    return paint;
  }

  @override
  void paint(Canvas canvas, Size size) {
    //绘制逻辑
    double wheelSize = min(size.width, size.height) / 2; //饼图的尺寸
//    double nbElem = 6;//分成6份
//    double radius = (2 * pi) / nbElem;//1/6圆
    //包裹饼图这个圆形的矩形框
    Rect boundingRect = Rect.fromCircle(
        center: Offset(wheelSize, wheelSize), radius: wheelSize);
    // 每次画1/6个圆弧
    double total = a.reduce((combine, value) => combine + value);
    for (var i = 0; i < a.length; i++) {
      List<double> x = a.sublist(0, i);

      double startRadius;
      if (x.length != 0) {
        startRadius = x.reduce((a, value) => a + value) / total * 2 * pi;
      } else {
        startRadius = 0;
      }
      double sweepRadius = (a[i] / total) * 2 * pi;
      canvas.drawArc(
          boundingRect, startRadius, sweepRadius, true, getColoredPaint());
    }
  }

  // 判断是否需要重绘，这里我们简单的做下比较即可
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => oldDelegate != this;
}

class Cake extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(200, 200),
      painter: WheelPainter(a: [10, 20, 30, 35, 5, 90, 30]),
    );
  }
}
